package com.jtny.nytb.equipment.utils;


import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class DealPageData {

    public static final int MAX_PAGE = 1000;
    public static final int MIN_PAGE = 500;

    //并行处理
    public static <T, R> void parallelDeal(List<T> list, Function<List<List<T>>, R> fun) {
        //内层为500条数据
        List<List<T>> lists = new ArrayList<>();
        int size = list.size();
        if (size >= MAX_PAGE) {
            int step = size / MIN_PAGE;
            if (size % MIN_PAGE != 0) {
                step = step + 1;
            }
            for (int i = 0; i < step; i++) {
                //本次处理的数据
                List<T> deals;
                if (i == step - 1) {
                    deals = list.subList(MIN_PAGE * i, size);
                } else {
                    deals = list.subList(MIN_PAGE * i, (i + 1) * MIN_PAGE);
                }
                lists.add(deals);
            }
            fun.apply(lists);
        } else {
            lists.add(list);
            fun.apply(lists);
        }
    }
}

